var typeLib = require('../type')
var pathLib = require('path')

// es6 模块的结构 export default arrayUnion
// var arrayUnion = require('array-union')
var union = require('arr-union')

/**
 * 充分利用ES6中的数组特性
 * @param  {...[type]}  arugments_    [description]
 * @return {[type]}     [description]
 */
function arrayUnion(... arugments_) {
	// Array.prototype.flat 新版本浏览器中才实现该方法
	if(Array.prototype.flat) {
		return [... new Set(arugments_.flat())]
	} else {
		var arrayFlat = require('./04-arrayFlat')
		return [... new Set( arrayFlat(arugments_)) ]
	}
}





function arrayUnionV2( arr, ... arugments_) {
	if(!Array.isArray(arr)) {
		throw new TypeError('Excepts the first argument is array')
	}

	var one 
	for (var i = 0; i < arugments_.length; i++) {
		one = arugments_[i]
		if(Array.isArray(one)) {
			arrayUnionV2(arr, ...one)
		} else {
			if(!arr.includes(one)) {
				arr.push(one)
			}
		}
	}

	return arr
}


function arrayUnionV3( init ) {
	if(!Array.isArray(init)) {
		throw new TypeError('Excepts the first argument is array')
	}

	var one 
	for (var i = 1; i < arguments.length; i++) {
		one = arguments[i]
		// 将每个元素统一转换为数组处理
		if(!Array.isArray(init)) {
			one = [one]
		}
		for (var j = 0; j < one.length; j++) {
			if(init.includes(one[j])){
				continue
			}
			init.push(one[j])
		}
	}

	return init
}

var array = ['a', {a: 'b'}, 1, 'b', 2, {c: 'd'}, 'c']


console.group('arr-union')
console.log( union(['a'], ['b', 'c', 'a'], ['d','b','a', 'e', 'f']) );
console.groupEnd()

console.log('\n-----------------------\n')

console.group(pathLib.basename(__filename))
console.log( arrayUnion(['a'], ['b', 'c', 'a'], ['d','b','a', 'e', 'f']) );
console.log( arrayUnionV2(['a'], ['b', 'c', 'a'], ['d','b','a', 'e', 'f']) );
console.log( arrayUnionV3(['a'], ['b', 'c', 'a'], ['d','b','a', 'e', 'f']) );
console.groupEnd()

